package com.example.demo.service;

import com.example.demo.mapper.MenuMapper;
import com.example.demo.pojo.Menu;
import com.example.demo.pojo.MenuExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ResourceService {

    @Autowired
    private MenuMapper menuMapper;

    //查询所有
    public List<Menu> getAllMenu(){
        MenuExample menuExample = new MenuExample();

        List<Menu> menus = menuMapper.selectByExample(menuExample);

        return menus;

    }


    //多条件分页查询
    public PageInfo<Menu> findByName(Integer pageNum,Integer pageSize,String name){

        //多条件
        MenuExample menuExample = new MenuExample();

        //创建条件容器
        MenuExample.Criteria criteria = menuExample.createCriteria();

        //模糊查询条件
        //添加按资源名字查询方法
        //resourceName.trim()去除字符串头尾空格
        if (!"".equals(name.trim()) && name.trim() != null){
            criteria.andM_chineseNameLike("%"+name.trim()+"%");
        }

        //PageHelper分页代码时的PageHelper.startPage(pageNum,pageSize);必须写在查询方法的上一句，中间不能添加任何代码，否则失效
        PageHelper.startPage(pageNum,pageSize);
        List<Menu> menus = menuMapper.selectByExample(menuExample);
        if (menus != null){
            return new PageInfo<>(menus);
        }

        return null;
    }

    //资源唯一性检查
    public List<Menu> getCName(Menu menu){

        return menuMapper.getCName(menu);
    }

    //添加资源
    public int addMenu(Menu menu){

        return menuMapper.insert(menu);

    }

    //通过id查询数据
    public Menu getInfoById(int id){

        return menuMapper.selectByPrimaryKey(id);
    }

    //更新资源
    public int updateMemu(int id,String menuName,String menuURL,int state){

        Menu oldMenu = menuMapper.selectByPrimaryKey(id);

        //通过前端数据修改对象数据
        oldMenu.setM_chineseName(menuName);
        oldMenu.setM_name(menuURL);
        oldMenu.setM_state(state);

        return menuMapper.updateByPrimaryKey(oldMenu);

    }

    //删除资源
    public int delete(int id){

        return menuMapper.deleteByPrimaryKey(id);
    }



}
